import React from 'react';
import { Tree, Input, Empty } from 'antd';

const { Search } = Input;

class SearchTree extends React.Component {
  state = {
    expandedKeys: [],
    searchValue: '',
    autoExpandParent: true,
  };

  dataList = [];

  componentDidUpdate() {
    const { TreeList } = this.props;
    if (!this.dataList.length) {
      this.generateList(TreeList);
    }
  }

  onExpand = (expandedKeys) => {
    this.setState({
      expandedKeys,
      autoExpandParent: false,
    });
  };

  onChange = (e) => {
    const { TreeList } = this.props;
    const { value } = e.target;
    if (!value) return;
    const expandedKeys = this.dataList
      .map((item) => {
        if (item.title.indexOf(value) > -1) {
          return this.getParentKey(item.value, TreeList);
        }
        return null;
      })
      .filter((item, i, self) => item && self.indexOf(item) === i);

    this.setState({
      expandedKeys,
      searchValue: value,
      autoExpandParent: true,
    });
  };

  generateList = (data) => {
    for (let i = 0; i < data.length; i++) {
      const node = data[i];
      const { value, title } = node;
      this.dataList.push({ value, title });
      if (node.children) {
        this.generateList(node.children);
      }
    }
  };

  getParentKey = (key, tree) => {
    let parentKey;
    for (let i = 0; i < tree.length; i++) {
      const node = tree[i];
      if (node.children) {
        if (node.children.some((item) => item.value === key)) {
          parentKey = node.value;
        } else if (this.getParentKey(key, node.children)) {
          parentKey = this.getParentKey(key, node.children);
        }
      }
    }
    return parentKey;
  };

  render() {
    const { TreeList, setPID, selectedKeys } = this.props;
    const { searchValue, expandedKeys, autoExpandParent } = this.state;
    const loop = (data) =>
      data.map((item) => {
        const index = item.title.indexOf(searchValue);
        const beforeStr = item.title.substr(0, index);
        const afterStr = item.title.substr(index + searchValue.length);
        const title =
          index > -1 ? (
            <span>
              {beforeStr}
              <span className="site-tree-search-value">{searchValue}</span>
              {afterStr}
            </span>
          ) : (
            <span>{item.title}</span>
          );
        if (item.children) {
          return { title, key: item.value, children: loop(item.children) };
        }

        return {
          title,
          key: item.value,
        };
      });

    if(!TreeList.length){
      return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
    }
    return (
      <div>
        <Search
          style={{ marginBottom: 8 }}
          placeholder="请输入关键字"
          allowClear
          onChange={this.onChange}
        />
        <Tree
          selectedKeys={selectedKeys}
          onExpand={this.onExpand}
          expandedKeys={expandedKeys}
          autoExpandParent={autoExpandParent}
          treeData={loop(TreeList)}
          onSelect={setPID}
        />
      </div>
    );
  }
}

export default SearchTree;
